package com.lcx.util;

import java.io.*;
import java.nio.charset.Charset;
import java.util.LinkedList;
import java.util.List;
import java.util.function.Predicate;

public class CopyFileUtil {

    /**
     * 将文件复制到指定目录
     * @param in
     * @param out 必须是文件夹
     */
    public static void copyAllFile(File in,File out) throws Exception{
        if(!out.exists()){
            out.mkdir();
        }
        if(out.isFile()){
            throw new RuntimeException("out is not a directory!");
        }
        if(in.isDirectory()){
            File d = new File(out.getAbsolutePath()+"/"+in.getName());
            d.mkdirs();
            File fs[] = in.listFiles();
            if(fs!=null){
                for(File f:fs){
                    copyAllFile(f,d);
                }
            }
        }else{
            copySimpleFile(in,new File(out.getAbsolutePath()+"/"+in.getName()));
        }
    }

    public static void copySimpleFile(File in,File out) throws Exception{
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            fis = new FileInputStream(in);
            fos = new FileOutputStream(out);
            byte b[] = new byte[1024];
            int read;
            while((read=fis.read(b))>-1){
                fos.write(b,0,read);
            }
            fos.flush();
        } catch (Exception e) {
        }finally {
            try {
                fos.close();
            }catch (Exception e){}
            try {
                fis.close();
            }catch (Exception e){}
        }
    }

    public static List<File> eachPath(File file, Predicate<File> filter){
        if(!file.exists()) return null;
        List<File> list = new LinkedList<>();
        if(filter == null || filter.test(file)) list.add(file);
        if(file.isDirectory()){
            File []listFiles = file.listFiles();
            List<File> ll;
            if(listFiles!=null){
                for(File f:listFiles){
                    ll = eachPath(f,filter);
                    if(ll != null) list.addAll(ll);
                }
            }
        }
        return list;
    }

    public static String readTextFromStream(InputStream is,String encoding){
        InputStreamReader isr = null;
        BufferedReader br = null;
        StringBuilder builder = new StringBuilder();
        try{
            isr = new InputStreamReader(is,encoding==null? Charset.defaultCharset().name():encoding);
            br = new BufferedReader(isr);
            String read;
            while((read=br.readLine())!=null){
                builder.append(read).append("\n");
            }
        }catch (Exception e){
            throw new RuntimeException(e);
        }finally {
            try {
                br.close();
            }catch (Exception e){}
            try {
                isr.close();
            }catch (Exception e){}
            try {
                is.close();
            }catch (Exception e){}
        }
        return builder.toString();
    }

}
